<template>
  <el-dialog
    v-el-drag-dialog
    :title="dataForm.activityId===0?'新增活动':'修改活动'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="时段名称" prop="activityTitle">
      <el-input v-model="dataForm.activityTitle" placeholder="整点抢购时段名称" maxlength="6"></el-input>
    </el-form-item>
    <el-form-item label="开始时间" prop="time">
      <template>
        <el-time-select
          placeholder="起始时间"
          v-model="startTime"
          :picker-options="{
          start: '00:00',
          step: '01:00',
          end: '24:00'
          }">
        </el-time-select>
        <el-time-select
          placeholder="结束时间"
          v-model="endTime"
          :picker-options="{
          start: '00:00',
          step: '01:00',
          end: '24:00',
          minTime: startTime
          }">
        </el-time-select>
      </template>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.publishRemark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        startTime: '',
        endTime: '',
        visible: false,
        dataForm: {
          activityId: 0,
          activityTitle: ''
        },
        dataRule: {
          activityTitle: [
            { required: true, message: '活动标题不能为空', trigger: 'blur' }
          ],
          startTime: [
            { required: true, message: '开始时间不能为空', trigger: 'blur' }
          ],
          endTime: [
            { required: true, message: '结束时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.activityId = id || 0
        this.visible = true
        // this.$nextTick(() => {
        //   this.$refs['dataForm'].resetFields()
        //   if (this.dataForm.activityId) {
        //     this.$http({
        //       url: this.$http.adornUrl(`/activity/spike/getActivityById/${this.dataForm.activityId}`),
        //       method: 'get',
        //       params: this.$http.adornParams()
        //     }).then(({data}) => {
        //       if (data && data.code === 0) {
        //         this.dataForm.activityNo = data.data.activityNo
        //         this.dataForm.activityTitle = data.data.activityTitle
        //         this.dataForm.publishRemark = data.data.publishRemark
        //         this.dataForm.time = [data.data.beginTime, data.data.endTime]
        //       }
        //     })
        //   }
        // })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/activity/spike/addWholePointActivities`),
              method: 'post',
              data: this.$http.adornData({
                'activityId': this.dataForm.activityId,
                'activityTitle': this.dataForm.activityTitle,
                'startTime': this.startTime,
                'endTime': this.endTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
